﻿#include"InventoryView.h"
#include"XQAlgorithm.h"
#include<QLineEdit>
#include<QComboBox>
#include<QCompleter>
#include<QBoxLayout>
#include<QPushButton>
#include<QLabel>
#include<QRegularExpressionValidator>
#include<QDoubleValidator>
#include<QDateTimeEdit>
#include<QMessageBox>
InventoryView::InventoryView(QWidget* parent)
	:XQOperateView(parent)
{
}
InventoryView::~InventoryView()
{
}

void InventoryView::setReadOnly(bool readOnly)
{
	for (int i = (int)Type::OkButtonType; i <= (int)Type::CtimeType; i++)
	{
		setReadOnly(i, readOnly);
	}
}

void InventoryView::setReadOnly(int type, bool readOnly)
{
	switch ((InventoryView::Type)type)
	{
	case InventoryView::Type::OkButtonType:
		transcribeWidget((int)XQOperateView::Type::OkButtonType)->setEnabled(!readOnly);
		break;
	case InventoryView::Type::CloseButtonType:
		transcribeWidget((int)XQOperateView::Type::CloseButtonType)->setEnabled(!readOnly);
		break;
	case InventoryView::Type::IdType:m_idEdit->setReadOnly(readOnly);
		break;
	case InventoryView::Type::NameType:m_nameEdit->setReadOnly(readOnly);
		break;
	case InventoryView::Type::TypeType:m_typeEdit->setEditable(!readOnly);
		break;
	case InventoryView::Type::CurrentInventoryType:m_currentInventoryEdit->setReadOnly(readOnly);
		break;
	case InventoryView::Type::SafetyInventoryType:m_safetyInventoryEdit->setReadOnly(readOnly);
		break;
	case InventoryView::Type::RestockingQuantityType:m_restockingQuantityEdit->setReadOnly(readOnly);
		break;
	case InventoryView::Type::MtimeType:m_mtimeEdit->setEnabled(!readOnly);
		break;
	case InventoryView::Type::CtimeType:m_ctimeEdit->setEnabled(!readOnly);
		break;
	default:
		break;
	}
}

InventoryModel InventoryView::inventoryModel() const
{
	InventoryModel model;
	model.setInventory_id(inventory_id());
	model.setType(type());
	model.setName(name());
	model.setCurrentInventory(currentInventory());
	model.setRestockingQuantity(restockingQuantity());
	model.setSafetyInventory(safetyInventory());
	model.setMTime(mtime());
	model.setCTime(ctime());
	return model;
}

quint64 InventoryView::inventory_id() const
{
	return m_idEdit->text().toULongLong();
}

QString InventoryView::name() const
{
	return m_nameEdit->text();
}

QString InventoryView::type() const
{
	return m_typeEdit->currentText();
}

quint64 InventoryView::currentInventory() const
{
	return m_currentInventoryEdit->text().toULongLong();
}

quint64 InventoryView::safetyInventory() const
{
	return m_safetyInventoryEdit->text().toULongLong();
}

quint64 InventoryView::restockingQuantity() const
{
	return m_restockingQuantityEdit->text().toULongLong();
}

QDateTime InventoryView::mtime() const
{
	return m_mtimeEdit->dateTime();
}

QDateTime InventoryView::ctime() const
{
	return m_ctimeEdit->dateTime();
}

void InventoryView::setInventoryModel(const InventoryModel& model)
{
	setInventory_id(model.inventory_id());
	setName(model.name());
	setCurrentInventory(model.currentInventory());
	setSafetyInventory(model.safetyInventory());
	setRestockingQuantity(model.restockingQuantity());
	setMTime(model.mtime());
	setCTime(model.ctime());
}

void InventoryView::setInventory_id(quint64 id)
{
	m_idEdit->setText(QString::number(id));
}

void InventoryView::setName(const QString& name)
{
	m_nameEdit->setText(name);
}

void InventoryView::setType(const QString& type)
{
	m_typeEdit->setCurrentText(type);
}

void InventoryView::setCurrentInventory(quint64 currentInventory)
{
	m_currentInventoryEdit->setText(QString::number(currentInventory));
}

void InventoryView::setSafetyInventory(quint64 safetyInventory)
{
	m_safetyInventoryEdit->setText(QString::number(safetyInventory));
}

void InventoryView::setRestockingQuantity(quint64 restockingQuantity)
{
	m_restockingQuantityEdit->setText(QString::number(restockingQuantity));
}

void InventoryView::setMTime(const QDateTime& time)
{
	m_mtimeEdit->setDateTime(time);
}

void InventoryView::setCTime(const QDateTime& time)
{
	m_ctimeEdit->setDateTime(time);
}

void InventoryView::setTypeCompleter(const QStringList& list)
{
	auto completer = m_typeEdit->completer();
	if (completer)
		completer->deleteLater();
	completer = new QCompleter(list, m_typeEdit);
	completer->setCaseSensitivity(Qt::CaseInsensitive); // 设置大小写不敏感
	completer->setCompletionMode(QCompleter::PopupCompletion); // 设置补全模式为弹出列表

	m_typeEdit->setCompleter(completer);
}

void InventoryView::setTypeItems(const QStringList& list)
{
	m_typeEdit->clear();
	m_typeEdit->addItems(list);
}

void InventoryView::setAutoRestockingQuantity(bool Auto)
{
	m_autoRestockingQuantity = Auto;
}

void InventoryView::init()
{
	XQOperateView::init();
	connect(m_currentInventoryEdit, &QLineEdit::textChanged, [=] {
		if(m_autoRestockingQuantity)
		{
			auto currentInventory = this->currentInventory();
			auto safetyInventory = this->safetyInventory();
			setRestockingQuantity(currentInventory< safetyInventory? safetyInventory- currentInventory:0);
		}
		
		});
	connect(m_safetyInventoryEdit, &QLineEdit::textChanged, [=] {
		if (m_autoRestockingQuantity)
		{
			auto currentInventory = this->currentInventory();
			auto safetyInventory = this->safetyInventory();
			setRestockingQuantity(currentInventory < safetyInventory ? safetyInventory - currentInventory : 0);
		}

		});
}

void InventoryView::okButtonSlots()
{
	if (isVisible(Type::IdType) && m_idEdit->text().isEmpty())
	{
		QMessageBox::information(this, "提示", "编号不可为空");
		return;
	}
	if (isVisible(Type::NameType) && name().isEmpty())
	{
		QMessageBox::information(this, "提示", "名称不可为空");
		return;
	}
	if (isVisible(Type::TypeType) && type().isEmpty())
	{
		QMessageBox::information(this, "提示", "类型不可为空");
		return;
	}
	if (isVisible(Type::CurrentInventoryType) && m_currentInventoryEdit->text().isEmpty())
	{
		QMessageBox::information(this, "提示", "当前库存不为空");
		return;
	}
	if (isVisible(Type::SafetyInventoryType) && m_safetyInventoryEdit->text().isEmpty())
	{
		QMessageBox::information(this, "提示", "安全库存不为空");
		return;
	}
	if (isVisible(Type::RestockingQuantityType) && m_restockingQuantityEdit->text().isEmpty())
	{
		QMessageBox::information(this, "提示", "补货量不为空");
		return;
	}
	emit okSignals();
}

void InventoryView::init_ui()
{
	XQWidget::init_ui();
	setCssFile(this, ":/XQUserInfoView/style.css");
	setWindowTitle("库存管理");
	//设置内容布局
	auto layout = new QBoxLayout(QBoxLayout::TopToBottom);
	layout->addWidget(init_id());
	layout->addWidget(init_name());
	layout->addWidget(init_type());
	layout->addWidget(init_currentInventory());
	layout->addWidget(init_safetyInventory());
	layout->addWidget(init_restockingQuantity());
	layout->addWidget(init_mtimeEdit());
	layout->addWidget(init_ctimeEdit());
	layout->addStretch(1);
	layout->addLayout(init_okAndClose());
	widget()->setLayout(layout);
}

QWidget* InventoryView::init_id()
{
	auto LeftLayout = new QBoxLayout(QBoxLayout::Direction::LeftToRight);//从左到右
	LeftLayout->addWidget(new QLabel("编号:"));
	m_idEdit = new QLineEdit();
	m_idEdit->setValidator(new QRegularExpressionValidator(QRegularExpression("^\\d+$"), m_idEdit));
	LeftLayout->addWidget(m_idEdit);

	auto widget = new QWidget();
	widget->setLayout(LeftLayout);
	widget->setAttribute(Qt::WA_TranslucentBackground, true);
	setTranscribeWidget(Type::IdType, widget);
	return widget;
}

QWidget* InventoryView::init_name()
{
	auto LeftLayout = new QBoxLayout(QBoxLayout::Direction::LeftToRight);//从左到右
	LeftLayout->addWidget(new QLabel("名称:"));
	m_nameEdit = new QLineEdit();
	LeftLayout->addWidget(m_nameEdit);

	auto widget = new QWidget();
	widget->setLayout(LeftLayout);
	widget->setAttribute(Qt::WA_TranslucentBackground, true);
	setTranscribeWidget(Type::NameType, widget);
	return widget;
}

QWidget* InventoryView::init_type()
{
	auto LeftLayout = new QBoxLayout(QBoxLayout::Direction::LeftToRight);//从左到右
	LeftLayout->addWidget(new QLabel("类型:"));
	m_typeEdit = new QComboBox();
	LeftLayout->addWidget(m_typeEdit, 1);
	m_typeEdit->setEditable(true);

	auto widget = new QWidget();
	widget->setLayout(LeftLayout);
	widget->setAttribute(Qt::WA_TranslucentBackground, true);
	setTranscribeWidget(Type::TypeType, widget);
	//m_widgets[(int)Type::TypeType] = widget;
	return widget;
}

QWidget* InventoryView::init_currentInventory()
{
	auto LeftLayout = new QBoxLayout(QBoxLayout::Direction::LeftToRight);//从左到右
	LeftLayout->addWidget(new QLabel("当前库存:"));
	m_currentInventoryEdit = new QLineEdit();
	m_currentInventoryEdit->setValidator(new QRegularExpressionValidator(QRegularExpression("^\\d+$"), m_currentInventoryEdit));
	LeftLayout->addWidget(m_currentInventoryEdit);

	auto widget = new QWidget();
	widget->setLayout(LeftLayout);
	widget->setAttribute(Qt::WA_TranslucentBackground, true);
	setTranscribeWidget(Type::CurrentInventoryType, widget);
	return widget;
}

QWidget* InventoryView::init_safetyInventory()
{
	auto LeftLayout = new QBoxLayout(QBoxLayout::Direction::LeftToRight);//从左到右
	LeftLayout->addWidget(new QLabel("安全库存:"));
	m_safetyInventoryEdit = new QLineEdit();
	m_safetyInventoryEdit->setValidator(new QRegularExpressionValidator(QRegularExpression("^\\d+$"), m_safetyInventoryEdit));
	LeftLayout->addWidget(m_safetyInventoryEdit);

	auto widget = new QWidget();
	widget->setLayout(LeftLayout);
	widget->setAttribute(Qt::WA_TranslucentBackground, true);
	setTranscribeWidget(Type::SafetyInventoryType, widget);
	return widget;
}

QWidget* InventoryView::init_restockingQuantity()
{
	auto LeftLayout = new QBoxLayout(QBoxLayout::Direction::LeftToRight);//从左到右
	LeftLayout->addWidget(new QLabel("补货量:"));
	m_restockingQuantityEdit = new QLineEdit();
	m_restockingQuantityEdit->setValidator(new QRegularExpressionValidator(QRegularExpression("^\\d+$"), m_restockingQuantityEdit));
	LeftLayout->addWidget(m_restockingQuantityEdit);

	auto widget = new QWidget();
	widget->setLayout(LeftLayout);
	widget->setAttribute(Qt::WA_TranslucentBackground, true);
	setTranscribeWidget(Type::RestockingQuantityType, widget);
	return widget;
}

QWidget* InventoryView::init_mtimeEdit()
{
	auto LeftLayout = new QBoxLayout(QBoxLayout::Direction::LeftToRight);//从左到右
	auto label = new QLabel("修改时间:", this);
	LeftLayout->addWidget(label, 0, Qt::AlignVCenter | Qt::AlignLeft);

	m_mtimeEdit = new QDateTimeEdit(this);//
	LeftLayout->addWidget(m_mtimeEdit);
	m_mtimeEdit->setDisplayFormat("yyyy-MM-dd hh:mm:ss");
	m_mtimeEdit->setCalendarPopup(true);
	//m_mtimeEdit->setStyleSheet("background-color: white;");
	auto widget = new QWidget();
	widget->setLayout(LeftLayout);
	widget->setAttribute(Qt::WA_TranslucentBackground, true);
	setTranscribeWidget(Type::MtimeType, widget);
	return widget;
}

QWidget* InventoryView::init_ctimeEdit()
{
	auto LeftLayout = new QBoxLayout(QBoxLayout::Direction::LeftToRight);//从左到右
	auto label = new QLabel("创建时间:", this);
	LeftLayout->addWidget(label, 0, Qt::AlignVCenter | Qt::AlignLeft);

	m_ctimeEdit = new QDateTimeEdit(this);//
	LeftLayout->addWidget(m_ctimeEdit);
	m_ctimeEdit->setDisplayFormat("yyyy-MM-dd hh:mm:ss");
	m_ctimeEdit->setCalendarPopup(true);
	auto widget = new QWidget();
	widget->setLayout(LeftLayout);
	widget->setAttribute(Qt::WA_TranslucentBackground, true);
	setTranscribeWidget(Type::CtimeType, widget);
	//m_widgets[(int)Type::CtimeType] = widget;
	return widget;
}
